<?php

namespace Modules\Order\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

// use Illuminate\Notifications\Messages\NexmoMessage;
use Modules\Core\Channels\MiniappSubscribeMessageChannel;

use Miniapp;
use Setting;

class OrderPaid extends Notification
{
    use Queueable;

    public $order;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($order)
    {
        $this->order = $order;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        $channels = [
            MiniappSubscribeMessageChannel::class
        ];

        return $channels;
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', 'https://laravel.com')
                    ->line('Thank you for using our application!');
    }

    /**
     * 获取通知的 Nexmo / 短信展示方式
     *
     * @param  mixed  $notifiable
     * @return NexmoMessage
     */
    public function toNexmo($notifiable)
    {
        return (new NexmoMessage)
        ->content('Your SMS message content');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }

    /**
     * 获取通知的模板消息
     *
     * @param  mixed  $notifiable
     * @return VoiceMessage
     */
    public function toMiniappSubscribeMessage($notifiable)
    {
        $order = $this->order;
        $page = 'pages/orderDetail/index?uuid=' . $order->uuid;

        $paidAt = $order->money_paid_at ?: $order->score_paid_at;

        $payPrice = '0元';
        if ($order->pay_money_price) {
            $payPrice = $order->pay_price / 100 . '元';
        }

        $coinPrice = '无';
        if ($order->pay_score_price) {
            $coinPrice = '使用' . $order->pay_score_price . (@Setting::get('sys_score')['alias'] ?: '积分') . '支付';
        }

        $message = [
            'page' => $page,
            'data' => [
                'date1' => [
                    'value' => (String)$paidAt
                ],
                'amount2' => [
                    'value' => $payPrice
                ],
                'character_string3' => [
                    'value' => $order->number
                ],
                'thing5' => [
                    'value' => $coinPrice
                ],
                'thing6' => [
                    'value' => $order->title()
                ],
                'thing7' => [
                    'value' => '前往小程序关注后续发货状态哦~'
                ],
            ],
        ];

        $miniapp = Miniapp::get($order->client_name);

        return [
            $miniapp,
            'order_paid',
            $message
        ];
    }
}
